/* eslint valid-jsdoc: "off" */

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = (appInfo) => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = (exports = {});
  const path = require("path");

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + "_1701327844496_2280";

  // add your middleware config here
  config.middleware = ["errorHandler", "tokenHandler"];

  config.tokenHandler = {
    // 路由为/api/role才使用中间件
    match: [
      "/api/user/info",
      "/api/user/edit",
      "/api/user/logout",
      "/api/role/add",
      "/api/role/del/:id",
      "/api/role/edit/:id",
      "/api/member/add",
      "/api/member/del/:id",
      "/api/member/edit/:id"
    ]
    // 路由为/api/role不使用中间件
    // ignore: ["/api/role"]
  };

  // 数据库相关
  config.sequelize = {
    dialect: "mysql", // support: mysql, mariadb, postgres, mssql
    database: "des",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "123456",
    // 配置数据库时间为东八区北京时间
    timezone: "+08:00",
    define: {
      // model的全局配置
      timestamps: true, // 不添加create,update,delete时间戳
      // 字段生成软删除时间戳 deleted_at
      paranoid: false,
      createdAt: "created_time",
      updatedAt: "updated_time",
      // deletedAt: 'deleted_at',
      freezeTableName: true, // 防止修改表名为复数
      underscored: false // 防止驼峰式字段被默认转为下划线
    },
    // 打印日志
    logging: true,
    // 时间格式化
    dialectOptions: {
      // 让读取date类型数据时返回时间戳而不是UTC时间
      dateStrings: true,
      typeCast: true
    },
    // 连接池
    pool: {
      max: 5,
      min: 0,
      idle: 10000
    }
  };

  // 关闭csrf
  config.security = {
    csrf: {
      enable: false
    },
    // 跨域白名单：网页端基地址
    domainWhiteList: []
  };

  // 跨域
  config.cors = {
    origin: "*",
    allowMethods: "GET, PUT, POST, DELETE, PATCH"
  };

  // 参数验证
  config.valparams = {
    locale: "zh-cn",
    throwError: true
  };

  // 数据加密
  config.crypto = {
    secret: "aef0a5c3-3daf-4ac5-b689-7b8b8f116f3e"
  };

  config.jwt = {
    // token 密码
    secret: "5dcbe1bb-b470-4d4d-9fb1-1b8379e75e0d", // 可以自定义
    sign: {
      //jwt.sign(***,***,[options,***])方法中，options的默认设置可以在这里配置；
      // 过期时间8小时
      expiresIn: 8 * (60 * 60) //多少s后过期。actionToken.js中,jwt.sing(plyload,secret,{expiresIn:number})会被合并，调用时设置优先级更高;
    }
  };

  /**
   * 配置swagger
   * @property {String} dirScanner - 插件扫描的文档路径
   * @property {String} basePath - api前置路由
   * @property {Object} apiInfo - 可参考Swagger文档中的Info
   * @property {Array[String]} apiInfo - 可参考Swagger文档中的Info
   * @property {Array[String]} schemes - 访问地址协议http或者https
   * @property {Array[String]} consumes - contentType的集合
   * @property {Array[String]} produces - contentType的集合
   * @property {Object} securityDefinitions - 安全验证，具体参考swagger官方文档
   * @property {Boolean} enableSecurity - 是否使用安全验证
   * @property {Boolean} routeMap - 是否自动生成route
   * @property {Boolean} enable - swagger-ui是否可以访问
   */
  config.swaggerdoc = {
    dirScanner: "./app/controller",
    apiInfo: {
      title: "egg-swagger",
      description: "swagger-ui for egg",
      version: "1.0.0"
    },
    schemes: ["http"],
    // consumes: ['application/json'],
    // produces: ['application/json'],
    securityDefinitions: {
      // apikey: {
      //   type: 'apiKey',
      //   name: 'clientkey',
      //   in: 'header',
      // },
      // oauth2: {
      //   type: 'oauth2',
      //   tokenUrl: 'http://petstore.swagger.io/oauth/dialog',
      //   flow: 'password',
      //   scopes: {
      //     'write:access_token': 'write access_token',
      //     'read:access_token': 'read access_token',
      //   },
      // },
    },
    // enableSecurity: false,
    // enableValidate: true,
    routerMap: true,
    enable: true
  };

  config.multipart = {
    mode: "file",
    fileSize: 1048576000,
    whitelist: [
      ".txt",
      ".png",
      ".jpg",
      ".jpeg",
      ".gif",
      ".bmp",
      ".webp",
      ".wbmp",
      ".tif",
      ".psd",
      ".raw",
      ".aac",
      ".mp"
    ]
  };

  config.uploadDir = "app/public/uploads/";

  config.static = {
    prefix: "/app/public", //访问前缀
    dir: path.join(appInfo.baseDir, "app/public"),
    dynamic: true,
    preload: false,
    maxAge: 31536000,
    buffer: true
  };

  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

  return {
    ...config,
    ...userConfig
  };
};
